package com.dldata.devtools30.controller.sysform;

import com.alibaba.fastjson.JSON;
import com.dldata.devtools30.common.EnumUtils;
import com.dldata.devtools30.entity.DevtFormListtemplateEntity;
import com.dldata.devtools30.entity.DevtFormMetadataEntity;
import com.dldata.devtools30.entity.DevtFormTypeEntity;
import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.repository.sysform.DevtFormMetadataDao;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTableService;
import com.dldata.devtools30.service.sysform.DevtFormListTemplateService;
import com.dldata.devtools30.service.sysform.DevtFormTypeService;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;

/**

 */
@Controller
@RequestMapping(value = "/V_DevtFormListTemplateController")
public class V_DevtFormListTemplateController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DevtFormListTemplateService sysCrfFormListTemplateService;
    
    @Autowired
    DevtFormMetadataDao sysCrfFormMetadataDao;
    @Autowired
    DevtModelMetadataTableService metadataTableService;
    
    @Autowired
    private DevtFormTypeService sysCrfFormTypeService;


    @RequestMapping(value = "/toPage")
    public String toPage(Model model){

        return "thymeleaf/devtools30/sysform/v_SysFormListTemplate";
    }

    @RequestMapping(value = "/optPage")
    public String optPage(Model model,String id){
        DevtFormListtemplateEntity sysCrfFormListtemplateEntityTmp = sysCrfFormListTemplateService.getTemplateById(id);

        model.addAttribute("SysFormListtemplateEntity", StringUtil.escape(JSON.toJSONString(sysCrfFormListtemplateEntityTmp)));
        return "thymeleaf/devtools30/sysform/v_SysFormListTemplateOpt";
    }

    @RequestMapping(value = "/toEdit")
    public String toEdit(  String id,Model model){
        DevtFormListtemplateEntity entity=null;
        if(!StringUtil.isNullOrSpace(id)){
            entity = sysCrfFormListTemplateService.getTemplateById(id);
        }
        if(entity==null){
            entity =new DevtFormListtemplateEntity();
            entity.setId("");
            entity.setFlid("");
            entity.setThcolconf("11");
            entity.setThconf("11");
            entity.setXh(1);
            entity.setPageflag(1);
            entity.setName("");
        }
        List<DevtFormTypeEntity> sysCrfFormTypeEntities=sysCrfFormTypeService.getAllFormType();
        model.addAttribute("flList",sysCrfFormTypeEntities);
        model.addAttribute("data",entity);
        model.addAttribute("addBtnCfg", entity.getThconf() != null && entity.getThconf().length()>1?entity.getThconf().charAt(1)+"":"0");
        model.addAttribute("searchBtnCfg",entity.getThconf()!=null&&entity.getThconf().length()>0?entity.getThconf().charAt(0)+"":"0");
        model.addAttribute("delBtnCfg",entity.getThcolconf()!=null&&entity.getThcolconf().length()>1?entity.getThcolconf().charAt(1)+"":"0");
        model.addAttribute("editBtnCfg",entity.getThcolconf()!=null&&entity.getThcolconf().length()>0?entity.getThcolconf().charAt(0)+"":"0");
        model.addAttribute("importBtnCfg",StringUtil.isNullOrSpace(entity.getImportconf())?"0":entity.getImportconf());
    
        List<DevtModelMetadataTableEntity> tableServiceAll
                = metadataTableService.getAll(Arrays.asList(new String[]{EnumUtils.ModelTypeEnum.填报表单.name()}));
        model.addAttribute("mtableList",tableServiceAll);
    
        List<DevtFormMetadataEntity> formMetadataEntities = sysCrfFormMetadataDao.getDataByTempleid(id);
        model.addAttribute("mtableid",formMetadataEntities.size()>0?formMetadataEntities.get(0).getTableid():"");
        
        return "thymeleaf/devtools30/sysform/v_SysFormListTemplateEdit";
    }




    @RequestMapping(value = "/toClone")
    public String toClone(Model model, String templateId){
        model.addAttribute("templateId",templateId);
        return "thymeleaf/devtools30/sysform/v_SysFormTemplateClone";
    }

}
